package taller2.controller.modes;

import java.awt.Dimension;
import java.awt.event.MouseEvent;

import taller2.utils.Point;
import taller2.view.draws.DrawingArea;
import taller2.view.draws.GraphDrawer;

public class MoveMode implements Mode{
	
	// pressing the mouse button checks if there is a Node close enough
	// and selects it, to start moving it (but only if no other one is selected)
	public void whenPressed(MouseEvent arg0,DrawingArea area) {
		GraphDrawer drawer = area.getDrawer();
		drawer.unselectNode();
		
		if(drawer.noNodesSelected()){
			Point initMouse = new Point(arg0.getX(),arg0.getY());
		
			if(drawer.selectCloseByNode(initMouse))
				area.repaint();
		}
	}

	// releasing the mouse deselects the node
	public void whenReleased(MouseEvent arg0,DrawingArea area) {
		area.getDrawer().unselectNode();
		area.repaint();
	}

	// dragging a node changes its position
	// (this way of moving is harder, but it makes makes the 
	//  movement smoother than just changing to mouse position)
	public void whenDragged(MouseEvent arg0,DrawingArea area) {
		GraphDrawer drawer = area.getDrawer();
		if(!drawer.noNodesSelected()){
			Dimension dimDeDibujo = area.getSize();
			
			int nuevoX = arg0.getX();
			int nuevoY = arg0.getY();
						
			while (nuevoY > dimDeDibujo.getHeight() ){ nuevoY = nuevoY - 5;  }
			while (nuevoY < 0 ){ nuevoY = nuevoY + 5;  }
			while (nuevoX > dimDeDibujo.getWidth() ){ nuevoX = nuevoX - 5;  }
			while (nuevoX < 0 ){ nuevoX = nuevoX + 5;  }
			
			Point initMouse = new Point(nuevoX,nuevoY);
			drawer.moveSelectedNodeWithMouse(initMouse);

			area.repaint();
		}	
	}

}
